const express = require('express')
const fs = require('fs').promises
const path = require('path')
const router = express.Router()

// 工具函数：延迟指定毫秒
function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
}
let timer = 1000
router.post('/yield', async (req, res) => {
    try {
        await delay(timer) // 模拟 5 秒延迟
        const data = JSON.parse(await fs.readFile(path.join(__dirname, 'yield.json'), 'utf8'))
        res.json(data)
    } catch (err) {
        console.error(err)
        res.status(500).json({ error: 'Failed to load yield data' })
    }
})

router.post('/wip', async (req, res) => {
    try {
        await delay(timer) // 模拟 5 秒延迟
        const data = JSON.parse(await fs.readFile(path.join(__dirname, 'wip.json'), 'utf8'))
        res.json(data)
    } catch (err) {
        console.error(err)
        res.status(500).json({ error: 'Failed to load wip data' })
    }
})

router.post('/wipDetail', async (req, res) => {
    try {
        await delay(timer) // 模拟 5 秒延迟
        const data = JSON.parse(await fs.readFile(path.join(__dirname, 'wipDetail.json'), 'utf8'))
        res.json(data)
    } catch (err) {
        console.error(err)
        res.status(500).json({ error: 'Failed to load wip data' })
    }
})


module.exports = router